﻿package vsg.engine {

	import flash.external.ExternalInterface;
	import flash.utils.*;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.URLRequestHeader;
	import flash.net.URLLoader;
	import flash.events.*;
	
	
	// --- mark the url file package ----
	public class UrlFileLoader {
		
		protected var fullPath:String;
		
		private var handleMap:Object = {};
		
		// handle load the url file
		public function UrlFileLoader(newFullPath:String) {
			this.fullPath = newFullPath;
		}
		
		//  defined the hand
		public function addAfterLoadHandle(fn:Function , scope:Object) {
			
			if (! (fn is Function )) {
				throw new Error("The first parameter is type of function");
			}
			
			this.handleMap["afterLoade"] = {"fn":fn , "scope":scope};
		}
		
		
		public function addLoadFailHandle(fn:Function , scope:Object) {
			if (! (fn is Function )) {
				throw new Error("The first parameter is type of function");
			}
			
			this.handleMap["loadFail"] = {"fn":fn , "scope":scope};			
		}
		
		/**
		 * load the handle object 
		 */
		public function doLoad() {
			
			var $sendRequest:URLRequest = new URLRequest(); 
			var $loader:URLLoader = new URLLoader();

			try {

				$sendRequest.url = this.fullPath;
				$sendRequest.method = URLRequestMethod.POST; 
				
				// --- defined listener ---
				$loader.addEventListener(Event.OPEN, openHander);
				$loader.addEventListener(Event.COMPLETE, completeHandler);
				$loader.addEventListener(HTTPStatusEvent.HTTP_STATUS , httpStatusHandler);
				$loader.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler);

				$loader.load($sendRequest);	
				
			} catch (err: Error) {
				trace(err);
				//ExternalInterface.call(debugCallJsFunction, err.toString());
			} finally {
				$sendRequest = null;
				$loader = null;

			}			
		}
		
		
		
		/**
		 * http set request 
		 */

		/**
		 * invoke the complete handler for load complete  
		 */
		private function completeHandler(evt:Event) {
			var $obj = evt.target;
			
			var $source = $obj.data;
			var $bytesLoaded = $obj.bytesLoaded;
			var $bytesTotal = $obj.bytesTotal;
			var $dataFormat = $obj.dataFormat;
			
			var fun = this.handleMap["afterLoade"].fn;
			var scope = this.handleMap["afterLoade"].scope;
			fun.call( scope , $obj );
			
			

			/*
			// ---- load reload class ---
			this.dependentClasses = ScriptLoaderGlobalHandle.findUsingImport($source);
			
			// --- fire event on object --
			//events["invkeClassDepends"].call(events , classDepends);
			
			// --- add class in to queue
			var classSrc:String = ScriptLoaderGlobalHandle.scriptWithOutUsing($source);
			// --- add the array to index ---
			if (classSource == null) {
				classSource = classSrc;
			}
			
			// --- set the state ---
			this.cState = LOADED;
			*/

			
		}
		
		private function openHander(evt:Event) {
			//ExternalInterface.call("Vsg.FlashRemoteProxy.flash_debug","open daa");
		}
		
		private function ioErrorHandler(evt:IOErrorEvent) {
			var $result = {
				text:evt.text,
				target:evt.target
			}
			var fun = this.handleMap["loadFail"].fn;
			var scope = this.handleMap["loadFail"].scope;
			fun.call( scope , evt.target );			
			//ExternalInterface.call("VsEngineCom.flash_failureHandle", $result);
		}
		
		private function httpStatusHandler(evt:HTTPStatusEvent) {
			var $result = {
				status:evt.status,
				target:evt.target
			}
			//ExternalInterface.call("VsEngineCom.flash_httpStatus", $result);
		}			
		
	}
}